package cn.lopr.im.android.mvp.view.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import cn.lopr.im.android.R;


public class MenuBottomDialog extends BottomSheetDialogFragment {

    private String articleId;
    private BottomSheetDialog dialog;
    private Context context;
    private OnBottomDialogClickListener mOnClickListener;


    public MenuBottomDialog(Context context, String articleId) {
        this.context = context;
        this.articleId = articleId;
    }

    @SuppressLint({"Range", "SetTextI18n"})
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        if (getActivity() == null) return super.onCreateDialog(savedInstanceState);

        dialog = new BottomSheetDialog(getActivity(), R.style.BottomSheet);

        View root = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_menu, null);
        dialog.setContentView(root);

        initWidget();
        //设置宽度
        ViewGroup.LayoutParams params = root.getLayoutParams();
        params.height = (int) (0.2 * getResources().getDisplayMetrics().heightPixels);

        root.setLayoutParams(params);
        Window window = dialog.getWindow();
        if (window != null) {
            window.setWindowAnimations(R.style.BottomSheet);
            //WindowManager.LayoutParams params2 = window.getAttributes();
            //params2.dimAmount = 0.0f;
            //window.setAttributes(params2);
        }
        return dialog;
    }

    @SuppressLint("SetTextI18n")
    private void initWidget() {
        BottomSheetBehavior<FrameLayout> dialogBehavior = dialog.getBehavior();
        dialogBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        TextView tvDel = dialog.findViewById(R.id.tv_del);
        TextView tvDis = dialog.findViewById(R.id.tv_dis);
        tvDel.setOnClickListener(e -> {
            e.setTag(this.articleId);
            mOnClickListener.onClick(e, 0);
        });
        tvDis.setOnClickListener(e -> this.dismiss());
    }

    @Override
    public void show(@NonNull FragmentManager manager, @Nullable String tag) {
        //在show之前设置弹出动画
        super.show(manager, tag);
        //在show之后设置关闭动画
    }

    public void onClickListener(OnBottomDialogClickListener mOnClickListener) {
        this.mOnClickListener = mOnClickListener;
    }

    public interface OnBottomDialogClickListener {
        void onClick(View view, int section);

        void onLongClick(View view, int section);
    }
}

